package cn.tedu.boyou.acticle.service.impl;

import cn.tedu.boyou.acticle.domain.Sight;
import cn.tedu.boyou.acticle.mapper.SightMapper;
import cn.tedu.boyou.acticle.service.ISightService;
import cn.tedu.boyou.common.query.SightQuery;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StreamUtils;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

@Service
public class SightServiceImpl implements ISightService {
    @Autowired
    private SightMapper sightMapper;

    public PageInfo query(SightQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Sight> regions = sightMapper.selectForList(qo);
        return new PageInfo(regions);
    }

    public List<Sight> queryTop5(SightQuery qo) {
        return sightMapper.selectTop5(qo);

    }

    public Sight selectBySightId(Long sightId) {
        return sightMapper.selectByPrimarykey(sightId);
    }

    @Override
    public List<Sight> queryTop3(Long id, int num) {
        return sightMapper.selectByDestId(id,num);
    }
    public String getCityBySightId(Long sightId) {
        Sight sight = sightMapper.getCity(sightId);
        String cityname = sight.getDestination().getName();
        return cityname;
    }

    @Value("${weather.url}")
    private String weaUrl;

    @Value("${city.id}")
    private String cityId;
    @Value("${city.code}")
    private String cityCode;
    @Value("${weather.appkey}")
    private String appkey;

    public void sendWeather(String cityName) {

        //模拟浏览器发送http请求
        String resp = null;
        //请求路径
        try{

        URL url = new URL(weaUrl);

        //建立连接
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        //设置请求方法
        connection.setRequestMethod("GET");
        //设置请求参数
        StringBuilder stringBuilder = new StringBuilder(100);
        stringBuilder.append("city=").append(cityName).append("&cityid=")
                .append(cityId).append("&citycode=").append(cityCode).append("&appkey=").append(appkey);
        //默认是false，设置true表示输出参数，表示将stringBuilder拼接上url进行请求发送
        connection.setDoOutput(true);
        connection.getOutputStream().write(stringBuilder.toString().getBytes("UTF-8"));

        //请求连接
        connection.connect();
        //获取响应数据
        resp = StreamUtils.copyToString(connection.getInputStream(), Charset.forName("UTF-8"));
        }catch (Exception e){
            e.printStackTrace();

        }
        Map map = JSON.parseObject(resp, Map.class);
        Object result = map.get("result");
        System.out.println(result);
        if(map.get("msg").equals("查询成功")){
            System.out.println("oK");
        }
    }


}
